package com.android.sharecar.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.sharecar.model.FavoriteAddress;

/**
 * This Activity shows the favorite address of user.
 * @author xinyan
 * @date 2011-10-18
 */
public class FavoriteAddressActivity extends ListActivity {
	public static final String TAG = "FavoriteAddressActivity";

	// Members
	private static List<FavoriteAddress> mAddresses = new ArrayList<FavoriteAddress>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favorite_address);

		getAddressesFromServer();
		setListAdapter(new AddressListAdapter(this, R.layout.list_item_address,
				mAddresses));
	}

	private void getAddressesFromServer() {
		FavoriteAddress address;
		Bitmap icon;
		BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources()
				.getDrawable(R.drawable.icon);
		icon = bitmapDrawable.getBitmap();

		for (int i = 0; i < 5; i++) {
			address = new FavoriteAddress();

			address.setAddress("北京市昌平区回龙观镇");
			address.setAddressTitle("标题" + (i + 1));

			mAddresses.add(address);
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent();
		Bundle data = new Bundle();

		Log.v(TAG, "Address title:"
				+ mAddresses.get(position).getAddressTitle() + " | Address: "
				+ mAddresses.get(position).getAddress() + " ");

		data.putString(FavoriteAddress.TAG, mAddresses.get(position)
				.getAddress());
		intent.putExtras(data);

		this.setResult(RESULT_OK, intent);
		this.finish();
	}

	private final class AddressListAdapter extends BaseAdapter {
		private final Context mContext;
		private final Integer mResource;
		private final List<FavoriteAddress> mData;

		public AddressListAdapter(Context context, Integer resource,
				List<FavoriteAddress> value) {
			this.mContext = context;
			this.mResource = resource;
			this.mData = value;
		}

		@Override
		public int getCount() {
			Log.i(TAG, "getItem-----" + mData.size());
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			Log.i(TAG, "getItem-----" + position);
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			Log.i(TAG, "getItemId-----" + position);
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Log.i(TAG, "getView-----" + position + "---convertView--"
					+ convertView);

			View view = View.inflate(mContext, mResource, null);
			TextView addressTitle, address;

			ImageView icon = (ImageView) view
					.findViewById(R.id.address_iv_icon);
			address = (TextView) view.findViewById(R.id.address_tv_address);
			addressTitle = (TextView) view
					.findViewById(R.id.address_tv_addressTitle);

			FavoriteAddress favoriteAddress = mData.get(position);
			if (null != favoriteAddress) {
				Log.v(TAG, "Address title:" + favoriteAddress.getAddressTitle()
						+ " | Address: " + favoriteAddress.getAddress() + " ");

				addressTitle.setText(favoriteAddress.getAddressTitle());
				address.setText(favoriteAddress.getAddress());
			}
			return view;
		}

	}
}
